/*
 * @Author: nqx2007 fenglimima334@163.com
 * @Date: 2023-12-11 11:16:54
 * @LastEditors: nqx2007 fenglimima334@163.com
 * @LastEditTime: 2023-12-12 18:12:09
 * @FilePath: /operator-manager/mock/user.ts
 * @Description: 登录和找回密码的接口Mock
 */
import routes from '../config/routes';
import { baseApiPrefix } from '../src/services';

function flatTree(tree: any[], key: string) {
  const result: any[] = [];
  tree.forEach((item) => {
    if(item.access){
      result.push({[key]:item[key],type:1});
      if (item.routes && item.routes.length > 0) {
        result.push(...flatTree(item.routes, key));
      }
    }
  });
  return result;
}
const currentMenuRoute=routes.filter((item) =>!['/','/login','/find'].includes(item.path))
const menuList=flatTree(currentMenuRoute,'access')

export default {
  //登录
  [`POST ${baseApiPrefix}/user/login`]: (
    req: Record<string, any>,
    res: any,
  ) => {
    const { userName, passWord } = req?.body ?? {};
    if (userName === 'admin' && passWord === 'admin') {
      return res.json({
        success: true,
        data: {
          userName: 'admin',
          id: '111111',
          token: 'andfnkflf',
        },
      });
    }
    res.json({
      success: false,
      data: null,
      message: '用户名或者密码错误',
    });
  },
  //获取当前登录的用户信息
  [`${baseApiPrefix}/user/user_info`]: (req: any, res: any) => {
    res.json({
      success: true,
      data: {
        sysUserInfoVo :{
          userName: 'admin',
          id: '111111',
        },
        menuList: menuList,
      },
    });
  },
  //检查手机号是否存在
  [`POST ${baseApiPrefix}/user/check_mobile`]: (req: any, res: any) => {
    const { mobile } = req.body;
    if (mobile === '15336587459') {
      res.json({
        success: true,
        data: 'true',
        message: '',
      });
    } else {
      res.json({
        success: false,
        data: 'false',
        message: '手机号未注册',
      });
    }
  },
  //发送手机验证码
  [`${baseApiPrefix}/user/send_sms/:mobile`]: (req: any, res: any) => {
    res.json({
      success: true,
      data: null,
      message: '',
    });
  },
  //校验短信验证码
  [`POST ${baseApiPrefix}/user/check_code`]: (req: any, res: any) => {
    res.json({
      success: true,
      data: null,
      message: '',
    });
  },
  //重置密码
  [`POST ${baseApiPrefix}/user/update_pwd`]: (req: any, res: any) => {
    res.json({
      success: true,
      data: null,
      message: '',
    });
  },
  //获取左侧菜单
  [`${baseApiPrefix}/menu/list`]: (req: any, res: any) => {
    res.json({
      success: true,
      data: routes.filter((item) =>!['/','/login','/find'].includes(item.path),
      ),
      errorCode: 0,
    });
  },
};
